package cn.sumpu.app.wlcity.web.mvc.hotspring;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.sumpu.app.wlcity.biz.SeckillBiz;
import cn.sumpu.app.wlcity.domain.Seckill;
import cn.sumpu.app.wlcity.exception.AppBusinessException;
import cn.sumpu.app.wlcity.util.Common;

@Controller
public class SeckillspringController {
	@Autowired
	SeckillBiz seckillBiz;
	//加载秒杀列表页面
	@RequestMapping(value = "/seckill.htm")
	public String seckill(Integer page,String seckillType,HttpServletRequest request) {
		if(page == null){
			page = 1;
		} 
		if(seckillType == null){
			seckillType = "01";
		} 
		
		try { 
			List<Seckill> seckillListFive = seckillBiz.getSeckillListFive(page,seckillType); 
			int size = seckillListFive.size();
			int totalPage = (size + Common.PAGE_SIZE_SECKILLFIVE- 1) / Common.PAGE_SIZE_SECKILLFIVE;
			request.setAttribute("totalPage", totalPage);
			request.setAttribute("seckillListFive", seckillListFive);
			request.setAttribute("link", "hot.htm");
			request.setAttribute("currentPage", page);
		} catch (AppBusinessException e) {
			e.printStackTrace();
			return "hotspring/error";
		}
		return "hotspring/seckill/seckill";
	}
	//加载秒杀详细页面
	/*@RequestMapping(value = "/seckillChildren.htm")
	public String seckillChildren(String seckillId,HttpServletRequest request) {
		 
		try { 
			Seckill seckill =seckillBiz.selectSeckill(seckillId); 
			request.setAttribute("seckill", seckill);
			Boolean isOver =seckillBiz.isSeckillOver(seckillId); 
			request.setAttribute("isOver", isOver);
		} catch (AppBusinessException e) {
			e.printStackTrace();
			return "hotspring/error";
		}
		return "hotspring/seckill/seckillChildren";
	}*/
	//插入秒杀数据
	@RequestMapping(value = "/seckillChildrenPhone.htm",method = RequestMethod.POST)
	public String seckillChildrenPhone(String seckillId,String phoneNum,HttpServletRequest request) {
		 
		try { 
			if(phoneNum==null||phoneNum==""){
				request.setAttribute("value", "phoneError");
				Seckill seckill =seckillBiz.selectSeckill(seckillId); 
				request.setAttribute("seckill", seckill);
			}
			else{
			String value= seckillBiz.seckill(seckillId,phoneNum);
			request.setAttribute("value", value);
			Seckill seckill =seckillBiz.selectSeckill(seckillId); 
			request.setAttribute("seckill", seckill);
			}
		} catch (AppBusinessException e) {
			e.printStackTrace();
			return "hotspring/error";
		}
		return "hotspring/seckill/seckillSuccess";
		
	}
	
}
